﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace RENIT.WebParts.TagsCloud3D.Utils
{
    public class FeatureDeploymentHelper
    {
        protected SPFeatureDefinition Definition { get; private set; }

        protected SPFeature Feature { get; private set; }

        private SPWeb web;
        private SPSite site;

        public SPWeb Web
        {
            get
            {
                if (Scope == SPFeatureScope.Site && web == null)
                {
                    web = Site.RootWeb;
                }
                return web;
            }
        }

        public SPSite Site
        {
            get
            {
                if (Scope == SPFeatureScope.Web && site == null)
                {
                    site = Web.Site;
                }
                return site;
            }
        }

        public SPFeatureScope Scope { get; private set; }

        public FeatureDeploymentHelper(SPFeatureReceiverProperties properties)
        {
            Definition = properties.Definition;
            Feature = properties.Feature;
            Scope = properties.Definition.Scope;
            if (Scope == SPFeatureScope.Site)
            {
                site = Feature.Parent as SPSite;
            }
            else if (Scope == SPFeatureScope.Web)
            {
                web = Feature.Parent as SPWeb;
            }
        }

        public void DeployFileTo(SPListTemplateType type, string filePath, string targetName, params KeyValuePair<string, string>[] properties)
        {
            SPList list = Site.GetCatalog(type);
            DeployFileToDocLib(list, filePath, targetName, properties);
        }

        public void DeployFileTo(string listTitle, string filePath, string targetName, params KeyValuePair<string, string>[] properties)
        {
            SPList list = Web.Lists[listTitle];
            DeployFileToDocLib(list, filePath, targetName, properties);
        }

        public void UnDeployFileFrom(SPListTemplateType type, string targetName)
        {
            SPList list = Site.GetCatalog(type);
            DeleteFile(list, targetName);
        }

        public void UnDeployFileFrom(string listTitle, string targetName)
        {
            SPList list = Web.Lists[listTitle];
            DeleteFile(list, targetName);
        }


        protected virtual void DeleteFile(SPList list, string targetName)
        {
            var file = list.RootFolder.Files
                .OfType<SPFile>()
                    .FirstOrDefault(f => targetName.Equals(f.Name, StringComparison.InvariantCultureIgnoreCase));
            if (file != null)
            {
                file.Item.Delete();
            }
        }

        protected virtual void DeployFileToDocLib(SPList library, string filePath, string targetName, KeyValuePair<string, string>[] properties)
        {
            DeleteFile(library, targetName);
            var item = library.AddItem(library.RootFolder.Url, SPFileSystemObjectType.File, targetName);
            foreach (var pair in properties)
            {
                item[pair.Key] = pair.Value;
            }
            using (var stream = Definition.GetFile(filePath))
            {
                item.File.SaveBinary(stream);
            }
        }
    }
}
